package com.xsomnus.network_programming.netty.white;

import com.xsomnus.network_programming.lib.User;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/7/4 0004 15:14
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 */
public class ServerHandler1 extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        ByteBuf byteBuf = (ByteBuf) msg;
        final int length = byteBuf.readableBytes();
        byte[] bytes = new byte[length];
        byteBuf.readBytes(bytes);
        final User.HelloRequest helloRequest = User.HelloRequest.parseFrom(bytes);
        System.out.println(helloRequest);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }
}
